//
//  HUDAlert.swift
//  Swift-app开发基础架构
//
//  Created by pro on 2017/8/14.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

import UIKit

import MBProgressHUD

// 位置
enum AlertPosition {
    case top
    case center
    case bottom
}
// 状态
enum AlertStatus {
    case normal
    case success
    case failure
    case load
    case progress
}

typealias ProgressFetch = (Float, Bool) -> ()
typealias ProgressAction = (@escaping ProgressFetch) -> ()


class KZHud {
   
    // 设置弹框显示时间
    static let duration = 2.0
    
    static let window = kWindow
    
    static func hideHUD() {
        
        MBProgressHUD.hide(for: window, animated: true)
    }
    
    // 设置默认的hud属性
    static func setupHudDefaultConfig(_ hud: MBProgressHUD) {
        
        // 设置控件的颜色
        hud.contentColor = UIColor.white
        // 设置mask背景
        hud.backgroundView.backgroundColor = UIColor(white: 0, alpha: 0.2)
        // 设置text的颜色
        hud.label.font = kFont(14)
        hud.label.numberOfLines = 0
        // 设置detailText的颜色
        hud.detailsLabel.font = kFont(12)
        // 设置hud背景框的颜色
        hud.bezelView.color = UIColor(white: 0, alpha: 0.8)
        // 设置hud隐藏自动移除功能
        hud.removeFromSuperViewOnHide = true
        // 设置hud显示和隐藏动画
        hud.animationType = .zoom
    }
    
    
    
    // 展示的最底层方法
    static func show(text:      String,
                     hasMasker: Bool,
                     position:  AlertPosition,
                     status:    AlertStatus,
                     imageName: String?,
                     action:    ProgressAction?,
                     duration:  TimeInterval) -> MBProgressHUD {
        // 先隐藏先前的
        hideHUD()
        
        // 创建一个新的
        let hud = MBProgressHUD.showAdded(to: window, animated: true)
        
        // 设置默认属性
        setupHudDefaultConfig(hud)
        
        // 设置文本内容
        hud.label.text = text
        
        // 设置遮罩
        hud.isUserInteractionEnabled = hasMasker
//        hud.backgroundView.style = .blur
//        hud.backgroundView.blurEffectStyle = .prominent
        hud.backgroundView.backgroundColor = hasMasker ? UIColor(white: 0, alpha: 0.2) : UIColor.clear
        
        // 根据status设置hud的模式
        var mode: MBProgressHUDMode
        let successImageName = imageName ?? "alert_success"
        let failureImageName = imageName ?? "alert_failure"
        
        switch status {
            // 普通展示
        case .normal:
            mode = .text
            // 成功或失败
        case .success, .failure:
            mode = .customView
            var image: UIImage?
            if status == .success {
                image = UIImage(named: successImageName)
            } else {
                image = UIImage(named: failureImageName)
            }
            let customView = UIImageView(image: image)
            hud.customView = customView
            // 加载中
        case .load:
            mode = .indeterminate
            // 进度条
        case .progress:
            mode = .determinateHorizontalBar
        }
        hud.mode = mode
        
        // 根据position设置hud的位置
        var offset: CGFloat
        switch position {
        case .top:
            offset = -MBProgressMaxOffset
        case .center:
            offset = 0
        case .bottom:
            offset = MBProgressMaxOffset
        }
        hud.offset = CGPoint(x: 0.0, y: offset)
        
        // 如果是进度展示不需要自动隐藏，因为需要自己隐藏或展示其他
        if status != .progress {
            hud.hide(animated: true, afterDelay: duration)
        }
        
        return hud;
    }
    
    // 仅展示文字
    static func showOnlyText(text:      String,
                             hasMasker: Bool) {
        
        let _ = show(text: text, hasMasker: hasMasker, position: .center, status: .normal, imageName: nil, action: nil, duration: duration)
    }
    static func showOnlyText(text:      String) {
        
        let _ = show(text: text, hasMasker: false, position: .center, status: .normal, imageName: nil, action: nil, duration: duration)
    }
    
    
    // 展示成功
    static func showSuccess(text:       String,
                            hasMasker:  Bool) {
        
        let _ = show(text: text, hasMasker: hasMasker, position: .center, status: .success, imageName: nil, action: nil, duration: duration)
    }
    static func showSuccess(text:       String) {
        
        let _ = show(text: text, hasMasker: false, position: .center, status: .success, imageName: nil, action: nil, duration: duration)
    }
    
    // 展示失败
    static func showFailure(text:       String,
                            hasMasker:  Bool) {
        
        let _ = show(text: text, hasMasker: hasMasker, position: .center, status: .failure, imageName: nil, action: nil, duration: duration)
    }
    static func showFailure(text:       String) {
        
        let _ = show(text: text, hasMasker: false, position: .center, status: .failure, imageName: nil, action: nil, duration: duration)
    }
    
    
    // 展示加载
    static func showLoading(text:       String,
                            hasMasker:  Bool) {
        
        let _ = show(text: text, hasMasker: hasMasker, position: .center, status: .load, imageName: nil, action: nil, duration: duration)
    }
    static func showLoading(text:       String) {
        
        let _ = show(text: text, hasMasker: false, position: .center, status: .load, imageName: nil, action: nil, duration: duration)
    }
    
    
    // 进度条展示
    static func showProgress(text:       String,
                             hasMasker:  Bool) -> MBProgressHUD {
        
        return show(text: text, hasMasker: false, position: .center, status: .progress, imageName: nil, action: nil, duration: duration)

    }
   
}






